﻿using System;

namespace InsertionSort
{
    class InsertionSortTest
    {
        static void Main(string[] args)
        {
            int[] array = { 5, 7, 0, 3, 4, 2, 6, 1 };
            InsertionSort(array);
            foreach (var item in array)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
        }

        private static void InsertionSort(int[] array)
        {
            for (int i = 1; i < array.Length; i++)
            {
                int value = array[i];
                int j = i - 1;
                bool done = false;
                do
                {
                    if (array[j] > value)
                    {
                        array[j + 1] = array[j];
                        j = j - 1;
                        if (j < 0)
                        {
                            done = true;
                        }
                    }
                    else
                    {
                        done = true;
                    }
                }
                while(!done);
                array[j + 1] = value;
            }
        }
    }
}